package com.ostrich.note.ui.note.adapter;


import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ostrich.note.R;
import com.ostrich.note.databinding.ItemNoteDetailImageBinding;
import com.ostrich.note.databinding.ItemNoteImageBinding;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

public class NoteDetailImageAdapter extends RecyclerView.Adapter<NoteDetailImageAdapter.NoteImageViewHolder> {
    private List<String> mList = new ArrayList<>();
    private boolean flag;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    private NoteTypeEditAdapter.ItemChildClickListener mItemChildClickListener;

    public void setItemChildClickListener(NoteTypeEditAdapter.ItemChildClickListener itemChildClickListener) {
        mItemChildClickListener = itemChildClickListener;
    }
    //数据做刷新
    public void setData(List<String> list){
        if(list != null && list.size()>0){
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }

    }

    //添加一条数据
    public void setData(String path){
        mList.add(path);
        notifyItemInserted(mList.size()-0);
    }

    //数据做刷新
    public void deleteData(int position){
        mList.remove(position);
        notifyItemRemoved(position);
    }

    @NonNull
    @Override
    public NoteDetailImageAdapter.NoteImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemNoteDetailImageBinding itemNoteImageBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_note_detail_image, parent, false);
        return new NoteDetailImageAdapter.NoteImageViewHolder(itemNoteImageBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull NoteDetailImageAdapter.NoteImageViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.mItemNoteImageBinding.setPath(mList.get(position));
        holder.mItemNoteImageBinding.setFlag(flag);

        holder.mItemNoteImageBinding.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemChildClickListener != null){
                    mItemChildClickListener.onItemChildClick(v,position);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class NoteImageViewHolder extends RecyclerView.ViewHolder{
        private ItemNoteDetailImageBinding mItemNoteImageBinding;
        public NoteImageViewHolder(ItemNoteDetailImageBinding mItemNoteImageBinding) {
            super(mItemNoteImageBinding.getRoot());
            this.mItemNoteImageBinding = mItemNoteImageBinding;


        }
    }

    public interface ItemChildClickListener{
        void onItemChildClick(View view,int position);
    }


}
